package com.demo.commcial.config;

import cn.hutool.crypto.SecureUtil;
import com.demo.commcial.entity.AdminOper;
import com.demo.commcial.service.IAdminOperService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class MyShiroRealm extends AuthorizingRealm {
//    @Resource
//    private UserInfoService userInfoService;
    @Autowired
    private IAdminOperService adminOperService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("权限配置-->MyShiroRealm.doGetAuthorizationInfo()");
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
//        UserInfo userInfo  = (UserInfo)principals.getPrimaryPrincipal();
//        for(SysRole role:userInfo.getRoleList()){
//            authorizationInfo.addRole(role.getRole());
//            for(SysPermission p:role.getPermissions()){
//                authorizationInfo.addStringPermission(p.getPermission());
//            }
//        }
        return authorizationInfo;
    }

    /*主要是用来进行身份认证的，也就是说验证用户输入的账号和密码是否正确。*/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
            throws AuthenticationException {
        System.out.println("MyShiroRealm.doGetAuthenticationInfo()");
        //获取用户的输入的账号.
        String username = (String)token.getPrincipal();
        String password = new String((char[]) token.getCredentials());
        System.out.println("账户名称：" + username);
        System.out.println("密码：" + password);
        //通过username从数据库中查找 User对象，如果找到，没找到.
        //实际项目中，这里可以根据实际情况做缓存，如果不做，Shiro自己也是有时间间隔机制，2分钟内不会重复执行该方法
//        UserInfo userInfo = userInfoService.findByUsername(username);
        AdminOper adminOper = adminOperService.findByUsername(username);
        System.out.println("----->>userInfo= " + adminOper);
        if(adminOper == null){
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                adminOper, //用户名
//                userInfo.getPassword(), //密码
                adminOper.getLoginPassword(),
//                ByteSource.Util.bytes(userInfo.getCredentialsSalt()),//salt=username+salt
                getName()  //realm name
        );
        return authenticationInfo;

        //实际项目中，这里可以根据实际情况做缓存，如果不做，Shiro自己也是有时间间隔机制，2分钟内不会重复执行该方法
//        if(StringUtils.isNotBlank(username)) {
//
////            UserInfo userInfo = userInfoServiceImpl.findByUsername(username);
//            AdminOper adminOper = adminOperService.findByUsername(username);
//
//            if(adminOper == null){
//                System.out.println("用户不存在");
//                throw new UnknownAccountException("用户名或密码错误！");
//            }
//
//            System.out.println(SecureUtil.md5(password));
//            System.out.println(adminOper.getLoginPassword());
//            SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
//                    adminOper, //用户名
//                    adminOper.getLoginPassword(), //密码
////                    ByteSource.Util.bytes(username),//salt=username+salt
//                    getName()  //realm name
//            );
//            return authenticationInfo;
//        }
//        throw new UnknownAccountException("用户名或密码错误！");
    }

}
